package com.sqfw.project.wp.utils;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

public class ListUtils {
    /**
     * 分割工具：分割成N个List
     **/
    public static <T> List<List<T>> split(List<T> list , int offset ){
        return CollectionUtils.isEmpty( list ) ? null : executeSplit( list , offset );
    }

    private static synchronized <T> List < List<T> > executeSplit( List<T> list, int offset ) {
        int size = list.size();
        int initialCapacity = size / offset ;
        if ( size % offset != 0 ){
            initialCapacity++;
        }
        List < List<T> > lists = new ArrayList<>( initialCapacity );
        for ( int i = 1 ; i <= initialCapacity ; i++ ) {
            int limit = offset * i;
            if ( size < limit ){
                limit = size;
            }
            lists.add( list.subList( ( offset * ( i-1 ) ) , limit ) );
        }
        return lists;
    }

}
